// pages/award-list/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    awardList: [],
    statusType: ["优惠券", "积分"],
    currentType: 0,
    couponsNum: [0, 0],
    couponList: [],
    jifenList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAwards()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });


    if (curType == 0) {
      this.setData({
        awardList: this.data.couponList
      })
    } else {
      this.setData({
        awardList: this.data.jifenList
      })
    }
    
    this.onShow();
  },

  /**
   * 查看我的奖品
   */
  getAwards: function () {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/market/prize/userprizelist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log('prizelist')
        console.log(res.data)
        if (res.data.code >= app.globalData.successCode) {

          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else {
          let couponsNumTemp = that.data.couponsNum
          couponsNumTemp[0] = res.data.data.couponPrizeNum
          couponsNumTemp[1] = res.data.data.jifenPrizeNum

          that.setData({
            couponList: res.data.data.couponPrizes,
            jifenList: res.data.data.jifenPrizes,
            awardList: res.data.data.couponPrizes,
            couponsNum: couponsNumTemp
          })
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  toUse: function () {
    wx.switchTab({
      url: "/pages/index/index"
    })
  }
})